package getui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * geohash 转经纬度
 * 
 * @author 10960
 *
 */
public class GeoHashTwo {
	private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
	private static HashMap<Character, Integer> CHARSMAP;

	static {
		CHARSMAP = new HashMap<Character, Integer>();
		for (int i = 0; i < CHARS.length; i++) {
			CHARSMAP.put(CHARS[i], i);
		}
	}

	public static void main(String[] args) {
		long starttime = System.currentTimeMillis();
		System.out.println(starttime);
		String geoHash = "wx4en3s4";
		ArrayList<String> columnList = new ArrayList<String>();
		ArrayList<String> geoHashList = new ArrayList<String>();
		List<String> aList = rout("F:/360Downloads/ndrc_data/env_staff.xlsx", columnList);
		System.out.println(aList.size());
		for (int i = 0; i < aList.size(); i++) {
			// System.out.println(aList.get(i) + ",geohash");
			LocationBean bean = getLocation(aList.get(i));
			// System.out.println(bean.getLat() + ",lat," + bean.getLng() +
			// ",lng");
			System.out.println(bean.getLat() + "," + bean.getLng());
		}
		long end = System.currentTimeMillis();
		System.out.println(end);
		System.out.println(end - starttime);
	}

	public static List<String> rout(String rount, ArrayList<String> columnList) {
		synchronized (columnList) {
			try {
				File file = new File(rount);
				FileInputStream in = new FileInputStream(file);
				XSSFWorkbook wb = new XSSFWorkbook(in);
				// XSSFWorkbook wbq= new XSSFWorkbook()
				// 取得工作表
				Sheet sheet = wb.getSheetAt(0);
				int firstRowNum = sheet.getFirstRowNum();
				int lastRowNum = sheet.getLastRowNum();

				Row row = null;
				Cell cell_b = null;
				for (int i = firstRowNum; i <= lastRowNum; i++) {
					row = sheet.getRow(i); // 取得第i行
					cell_b = row.getCell(1); // 取得i行的第二列
					String cellValue = cell_b.getStringCellValue().trim();
					columnList.add(cellValue);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return columnList;
		}
	}

	/**
	 * @param geoHash
	 * @return
	 * @Author:lulei
	 * @Description: 返回geoHash 对应的坐标
	 */
	public static LocationBean getLocation(String geoHash) {
		String geoHashBinaryStr = getGeoHashBinaryString(geoHash);
		if (geoHashBinaryStr == null) {
			return null;
		}
		StringBuffer lat = new StringBuffer();
		StringBuffer lng = new StringBuffer();
		for (int i = 0; i < geoHashBinaryStr.length(); i++) {
			if (i % 2 != 0) {
				lat.append(geoHashBinaryStr.charAt(i));
			} else {
				lng.append(geoHashBinaryStr.charAt(i));
			}
		}
		double latValue = getGeoHashMid(lat.toString(), LocationBean.MINLAT, LocationBean.MAXLAT);
		double lngValue = getGeoHashMid(lng.toString(), LocationBean.MINLNG, LocationBean.MAXLNG);
		LocationBean location = new LocationBean(latValue, lngValue);
		location.setGeoHash(geoHash);
		return location;
	}

	/**
	 * @param geoHash
	 * @return
	 * @Author:lulei
	 * @Description: 将geoHash转化为二进制字符串
	 */
	private static String getGeoHashBinaryString(String geoHash) {
		if (geoHash == null || "".equals(geoHash)) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < geoHash.length(); i++) {
			char c = geoHash.charAt(i);
			if (CHARSMAP.containsKey(c)) {
				String cStr = getBase32BinaryString(CHARSMAP.get(c));
				if (cStr != null) {
					sb.append(cStr);
				}
			}
		}
		return sb.toString();
	}

	/**
	 * @param i
	 * @return
	 * @Author:lulei
	 * @Description: 将数字转化为二进制字符串
	 */
	private static String getBase32BinaryString(int i) {
		if (i < 0 || i > 31) {
			return null;
		}
		String str = Integer.toBinaryString(i + 32);
		return str.substring(1);
	}

	/**
	 * @param binaryStr
	 * @param min
	 * @param max
	 * @return
	 * @Author:lulei
	 * @Description: 返回二进制对应的中间值
	 */
	private static double getGeoHashMid(String binaryStr, double min, double max) {
		if (binaryStr == null || binaryStr.length() < 1) {
			return (min + max) / 2.0;
		}
		if (binaryStr.charAt(0) == '1') {
			return getGeoHashMid(binaryStr.substring(1), (min + max) / 2.0, max);
		} else {
			return getGeoHashMid(binaryStr.substring(1), min, (min + max) / 2.0);
		}
	}
}
